package com.application.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.MenuProvider;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.application.BR;
import com.application.R;
import com.application.adapter.MyRefreshAdapter;
import com.application.base.BaseFragment;
import com.application.bean.NewsBean;
import com.application.databinding.FragmentFindBinding;
import com.application.viewmodel.FindViewModel;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

public class FindFragment extends BaseFragment<FragmentFindBinding, FindViewModel> {
    private SmartRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private MyRefreshAdapter myRefreshAdapter;

    public FindFragment() {
    }
    public static FindFragment newInstance() {
        return new FindFragment();
    }
    @Override
    protected int initLayoutId() {
        return R.layout.fragment_find;
    }
    @Override
    protected int initVariableId() {
        return BR.findViewModel;
    }

    @Override
    protected void initData() {
        super.initData();
        binding.setFragment(this);
        initView();
        viewModel.init();
        initAdapter();
        initRefreshEvent();
    }

    @Override
    protected void initViewObservable() {
        super.initViewObservable();
        viewModel.layoutType.setValue(1);
        viewModel.layoutType.observe(getViewLifecycleOwner(),(Integer layoutType)->{
            if (recyclerView==null || getContext()==null){
                return;
            }
            RecyclerView.LayoutManager manager=null;
            switch (layoutType){
                case 1:
                    manager=new LinearLayoutManager(getContext());
                    break;
                case 2:
                    manager=new GridLayoutManager(getContext(),2);
                    break;
                case 3:
                    manager=new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
                    break;
            }
            if (manager!=null){
                recyclerView.setLayoutManager(manager);
            }
        });
        viewModel.newsList.observe(getViewLifecycleOwner(),newsBeans -> {
            if (myRefreshAdapter!=null && newsBeans!=null){
                myRefreshAdapter.setList(newsBeans);
            }
        });
        initMenu();
    }

    private void initView() {
        recyclerView = binding.recyclerView;
        refreshLayout = binding.recyclerRefresh;
        recyclerView.setOverScrollMode(RecyclerView.OVER_SCROLL_NEVER);
    }

    private void initAdapter() {
        myRefreshAdapter = new MyRefreshAdapter(viewModel.newsList.getValue());
        recyclerView.setAdapter(myRefreshAdapter);

        myRefreshAdapter.setOnItemClickListener((adapter, view, position) -> {
            NewsBean clickedNews = (NewsBean) adapter.getData().get(position);
            Toast.makeText(getContext(), "点击了:" + clickedNews.getTitle(), Toast.LENGTH_SHORT).show();
        });
    }

    private void initRefreshEvent() {
        refreshLayout.setOnRefreshListener(refreshLayout -> new Handler(Looper.getMainLooper()).postDelayed(() -> {
            viewModel.refreshData();
            refreshLayout.finishRefresh();
            Toast.makeText(getContext(), "刷新成功!", Toast.LENGTH_SHORT).show();
        }, 1000));

//        上拉加载
        refreshLayout.setOnLoadMoreListener(refreshLayout -> new Handler(Looper.getMainLooper()).postDelayed(() -> {
            boolean hasMore=viewModel.loadMore();
            if (hasMore) {
                refreshLayout.finishLoadMore();
                Toast.makeText(getContext(), "已加载第"+viewModel.currentPage+"页", Toast.LENGTH_SHORT).show();
            }else {
                refreshLayout.finishLoadMoreWithNoMoreData();
                Toast.makeText(getContext(),"已加载全部内容",Toast.LENGTH_SHORT).show();
            }
        }, 500));
    }

    private void initMenu() {
        requireActivity().addMenuProvider(
                new MenuProvider() {
                    @Override
                    public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
                        menuInflater.inflate(R.menu.menu_recycler_view, menu);
                    }

                    @Override
                    public boolean onMenuItemSelected(@NonNull MenuItem menuItem) {
                        viewModel.changeLayoutManger(menuItem.getItemId());
                        return true;
                    }
                }, getViewLifecycleOwner()
        );
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}